﻿package net.wangit.view.element.component;

import net.wangit.LabelUtil;
import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.framework.expression.LabelExpression;
import net.wangit.framework.util.MapListUtil;
import net.wangit.support.AbstractElementWriter;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.enumeration.EnumerationUtil;
import net.wangit.view.util.Html;

public class OrderElementWriter extends AbstractElementWriter {

	public OrderElementWriter() {
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeReadOnlyControl(ac, html, ec, row);
		String id = (new StringBuilder(String.valueOf(ec.getId())))
				.append(".o").toString();
		String alias = ElementUtil.getAlias(ec.getParent().getId(), id);
		String value = row != null ? row.get(id, "1") : "1";
		html.append(" <input type=\"radio\" name=\"").append(alias).append(
				"\" class=\"").append("E6").append("\" value=\"").append("1")
				.append("\"");
		Html.setChecked(html, value, "1");
		html.append("/>");
		html.append(LabelUtil.get(ac, "system", "query.order.asc"));
		html.append("<input type=\"radio\" name=\"").append(alias).append(
				"\" class=\"").append("E6").append("\" value=\"").append("2")
				.append("\"");
		Html.setChecked(html, value, "2");
		html.append("/>");
		html.append(LabelUtil.get(ac, "system", "query.order.desc"));
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		html.append("<select name=\"").append(ElementUtil.getAlias(ec)).append(
				"\"");
		Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
		Html.setCss(html, ec.getCss(), "E3");
		Html.setStyleWithWidth(html, ec.getWidth());
		Html.setSize(html, ec.getHeight());
		html.append(">");
		writeOptions(ac, html, ec, row);
		html.append("</select>");
	}

	protected void writeDisabledControl(ActionContext actioncontext,
			StringBuffer stringbuffer, ElementComponent elementcomponent,
			Row row1) {
	}

	protected void writeHidden(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		html.append("<div id=\"").append(ElementUtil.getAlias(ec)).append(
				"\" style=\"display:none\">");
		writeControl(ac, html, ec, row);
		html.append("</div>");
	}

	private void writeOptions(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		String config = LabelExpression.resolve(ac, ElementUtil.resolveCustom(
				ac, ec, row));
		MapList enumeration = MapListUtil.getEnumeration(config);
		if (!Checker.isEmpty(enumeration)) {
			enumeration.insertRow(0, EnumerationUtil.getBlankRow());
			String value = ElementUtil.getValueOrDefault(ac, ec, row);
			for (int i = 0; i < enumeration.size(); i++) {
				Row item = enumeration.getRow(i);
				String itemValue = item.get("value");
				html.append("<option value=\"").append(itemValue).append("\"");
				Html.setSelected(html, value, itemValue);
				html.append(">").append(item.get("name")).append("</option>");
			}

		}
	}
}
